package com.mayikt.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mayikt.base.BaseApiService;
import com.mayikt.base.BaseResponse;
import com.mayikt.entity.UnionLoginDO;
import com.mayikt.entity.UserDO;
import com.mayikt.mapper.UnionLoginMapper;
import com.mayikt.mapper.UserMapper;
import com.mayikt.service.MemberOpenIdTokenLogin;
import com.mayikt.strategy.UnionLoginStrategy;
import com.mayikt.utils.SpringContextUtils;
import com.mayikt.utils.TokenUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Name MemberOpenIdTokenLoginImpl
 * @Author 枫火
 * @Date 2021/5/29 15:32
 * @Description
 **/
@RestController
//@CrossOrigin
public class MemberOpenIdTokenLoginImpl extends BaseApiService implements MemberOpenIdTokenLogin {
    @Autowired
    private TokenUtil tokenUtil;

    @Value("${mayikt.login.token.prefix}")
    private String loginTokenPrefix;
    @Autowired
    private UnionLoginMapper unionLoginMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public BaseResponse<JSONObject> openIdLoginToken(String openToken) {
        if (StringUtils.isEmpty(openToken)) {
            return null;
        }
        // 1.根据openToken 获取真实openid
        String tokenValue = tokenUtil.getTokenValue(openToken);
        if (StringUtils.isEmpty(tokenValue)) {
            return setResultError("流程已经失效或者token错误");
        }
        // 2.根据openid 查询是否关联
        JSONObject jsonObject = JSONObject.parseObject(tokenValue);

        String unionPublicId = jsonObject.getString("unionPublicId");
        // 3.根据该渠道id查询bean的id，从容器中获取
        // 根据渠道id查询 联合基本信息  在缓存存一份
        UnionLoginDO unionLoginDo = unionLoginMapper.selectByUnionLoginId(unionPublicId);
        if (unionLoginDo == null) {
            return setResultError("该渠道可能已经关闭或者不存在");
        }
        String unionBeanId = unionLoginDo.getUnionBeanId();
        UnionLoginStrategy unionLoginStrategy = SpringContextUtils.
                getBean(unionBeanId, UnionLoginStrategy.class);
        if (unionLoginStrategy == null) {
            return setResultError("没有查询到该策略");
        }
        String openid = jsonObject.getString("openId");
//        UserDO userDO = null;
//        switch (unionPublicId) {
//            case "mayikt_qq":
//                userDO = userMapper.selectByQQOpenId(openid);
//            case "mayikt_weixin":
//                userDO = userMapper.selectByOpenId(openid);
//        }
        // 策略模式重构，以上两种场景方法分别放子类中实现
        UserDO userDo = unionLoginStrategy.getDbOpenId(openid);
        if (userDo == null) {
            return setResultError("当前用户没有关联，应该跳转到关联页面");
        }
        // 3.如果已经关联，自动帮助实现登录
        //获取userId 这里可以和MemberLoginServiceImpl共同业务逻辑重构一个manage
        Long userId = userDo.getUserId();
        String userToken = tokenUtil.createToken(loginTokenPrefix, userId + "");
        JSONObject data = new JSONObject();
        data.put("userToken", userToken);
        return setResultSuccess(data);
    }
}
